<!-- 我的备品 -->

<template>
  <WebTable path="deal" :list="one.repair.workOrderPreAssetDto" idKey="key" noPage auto hasSum :sumMethod="sumMethod">
    <RowText label="备品名称" bind="assetName" :minWidth="120" />

    <RowText label="规格型号" bind="modelName" :minWidth="100" empty="-" />

    <RowNum label="库存数量" bind="stockNum" />

    <RowSlot label="数量" :width="160">
      <template #default="{ row }">
        <!-- 出库数量 出库数量不能大于库存数量 -->
        <FormNum
          :one="getChild(row)"
          bind="number"
          :min="1"
          :max="row.stockNum"
          noLabel
          noMargin
          @change="changeNum(row)"
        />
      </template>
    </RowSlot>

    <RowText label="单位" bind="unit" :width="80" />

    <RowSlot label="单价 (元)">
      <template #default="{ row }">{{ CORE.numStr(row.unitPrice / 100) }}</template>
    </RowSlot>

    <RowSlot label="总价 (元)">
      <template #default="{ row }">{{ CORE.numStr(row.totalPrice / 100) }}</template>
    </RowSlot>

    <RowOperate :btns="['del']" :onDel="toDel" :width="80" />
  </WebTable>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyBackup',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb('deal')],

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 表格属性
     */
    tableProps() {
      const { one, sumMethod } = this
      return {
        list: one.workOrderPreAssetDto || [],
        noPage: true,
        hasSum: true,
        sumMethod,
      }
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 获取行数据的子项
     */
    getChild(row) {
      const { workOrderPreAssetDto: backup } = this.one.repair
      const matched = backup.find((item) => item.key === row.key) || {}
      return matched
    },

    /**
     * 点击删除
     */
    toDel(row) {
      const { workOrderPreAssetDto: backup } = this.one.repair
      const index = backup.findIndex((item) => item === row)
      backup.splice(index, 1)
    },

    /**
     * 改变数量
     */
    changeNum(row) {
      row.totalPrice = row.unitPrice * (row.number || 0)
    },

    /**
     * 合计方法
     */
    sumMethod(param) {
      const { columns, data } = param
      const sums = columns.map(() => '')
      sums[sums.length - 3] = '合计'
      sums[sums.length - 2] = data.reduce((total, row) => total + row.totalPrice, 0) / 100
      return sums
    },
  },
}
</script>
